/******************************************************************************
 * * mmi_modem_adaptor_video_call.c - implementation of modem adapter main functions
 *
 * *(C) Copyright 2019 Asr International Ltd.
 * * All Rights Reserved
 * ******************************************************************************/
#include "lv_watch.h"

#if USE_LV_WATCH_VOIP_ADAPTOR
#include "mmi_modem_adaptor_video_call.h"

#if LV_WATCH_AUTO_TEST != 0
static VOID MMI_ModemAdp_Video_Call_Test_Main(VOID * unused);
static TASK_HANDLE * modem_adp_video_test_task_id = NULL;
#endif

#define MODEM_ADP_TASK_PRIORITY 199
MODEM_VIDEO_CALL_REG_STAT g_ModemVcRegStat = MODEM_VIDEO_CALL_REG_FAIL;

static VOID MMI_ModemAdp_Video_Call_Main(VOID * para);

/**
 * initiate mmi-modem adaptor video call module
 * param (in) void:
 * return  void
 */
VOID MMI_ModemAdp_Video_Call_Init_Req(VOID)
{
    uos_create_task(MMI_ModemAdp_Video_Call_Main, NULL, 64, 4 * 1000, MODEM_ADP_TASK_PRIORITY, "modem_adpator_video_call");
}

/**
 * module entry
 * param (in) void
 * return  void
 */
static VOID MMI_ModemAdp_Video_Call_Main(VOID * para)
{
    (void)para;
    voipmsg_t voipmsg;

    while(gui_task_id == NULL) {
        printf("gui_task_id is NULL");
        uos_sleep(10);
    }

    // get msg
    while(1) {
        // receive message
        if(voipMsgQueReceive(&voipmsg)) {
            printf("voip Q is not ready, sleep for 500ms\n");
            uos_sleep(100);
            continue;
        }

        switch(voipmsg.msgid) {
            case VOIP_UA_EVENT_CALL_INCOMING:
                printf("%s voipmsg id is VOIP_UA_EVENT_CALL_INCOMING\n", __FUNCTION__);
                if((voipmsg.callname_present) && (voipmsg.uri_present)) {
                    MMI_Modem_Video_Call_Response_Incoming_t * Response;

                    Response = (MMI_Modem_Video_Call_Response_Incoming_t *)Hal_Mem_Alloc(
                                   sizeof(MMI_Modem_Video_Call_Response_Incoming_t));

                    strcpy(Response->callname, voipmsg.callname);
                    strcpy(Response->uri, voipmsg.uri);

                    if(VOIP_VOICE_CALL == voipmsg.calltype) {
                        Response->calltype = MODEM_VIDEO_CALL_TYPE_VOICE;
                    } else {
                        Response->calltype = MODEM_VIDEO_CALL_TYPE_VIDEO;
                    }

                    MMI_Modem_Video_Call_Response_Incoming(Response);
                } else {
                    printf("%s, invalid MT params", __FUNCTION__);
                    bsapi_call_action(BARESIPAPI_REJ, BARESIPVIDMODE_INV);
                }
                break;
            case VOIP_UA_EVENT_CALL_RINGING:
                printf("%s voipmsg id is VOIP_UA_EVENT_CALL_RINGING\n", __FUNCTION__);
                //play tone
                MMI_Modem_Video_Call_Common_Response(MMI_MODEM_VIDEO_CALL_RESPONSE_RING);
                break;
            case VOIP_UA_EVENT_CALL_ESTABLISHED:
                printf("%s voipmsg id is VOIP_UA_EVENT_CALL_ESTABLISHED\n", __FUNCTION__);
                MMI_Modem_Video_Call_Common_Response(MMI_MODEM_VIDEO_CALL_RESPONSE_CONNECT);
                break;
            case VOIP_UA_EVENT_CALL_CLOSED:
                printf("%s voipmsg id is VOIP_UA_EVENT_CALL_CLOSED\n", __FUNCTION__);
                if(0 == Hal_Pm_Get_State()) {
                    Hal_Pm_WakeUp();
                }

                MMI_Modem_Video_Call_Common_Response(MMI_MODEM_VIDEO_CALL_RESPONSE_DISCONNECT);
                break;
            case VOIP_UA_EVENT_CALL_TRANSFER:
                printf("%s voipmsg id is VOIP_UA_EVENT_CALL_TRANSFER\n", __FUNCTION__);
                break;
            case VOIP_UA_EVENT_REGISTER_OK:
                printf("%s voipmsg id is VOIP_UA_EVENT_REGISTER_OK\n", __FUNCTION__);
                g_ModemVcRegStat = MODEM_VIDEO_CALL_REG_OK;
                break;
            case VOIP_UA_EVENT_UNREGISTERING:
                printf("%s voipmsg id is VOIP_UA_EVENT_UNREGISTERING\n", __FUNCTION__);
                g_ModemVcRegStat = MODEM_VIDEO_CALL_REG_UNREGISTERING;
                break;
            case VOIP_UA_EVENT_REGISTER_FAIL:
                printf("%s voipmsg id is VOIP_UA_EVENT_REGISTER_FAIL\n", __FUNCTION__);
                g_ModemVcRegStat = MODEM_VIDEO_CALL_REG_FAIL;
                break;
            case VOIP_UA_EVENT_MWI_NOTIFY:
                printf("%s voipmsg id is VOIP_UA_EVENT_MWI_NOTIFY\n", __FUNCTION__);
                break;
            case VOIP_UA_EVENT_AUDIO_ERROR:
                printf("%s voipmsg id is VOIP_UA_EVENT_AUDIO_ERROR\n", __FUNCTION__);
                break;
            case VOIP_UA_EVENT_CALL_TERMINATE_BY_BOTTOM_LAYER:
                printf("%s voipmsg id is VOIP_UA_EVENT_CALL_TERMINATE_BY_BOTTOM_LAYER\n", __FUNCTION__);
                MMI_Modem_Video_Call_Common_Response(MMI_MODEM_VIDEO_CALL_RESPONSE_BOTTOM_ERR);
                break;
            case VOIP_UA_EVENT_CALL_NEED_REDIAL:
                printf("%s voipmsg id is VOIP_UA_EVENT_CALL_NEED_REDIAL\n", __FUNCTION__);
                MMI_Modem_Video_Call_Common_Response(MMI_MODEM_VIDEO_CALL_RESPONSE_NEED_REDIAL);
                break;
            case VOIP_UA_EVENT_AUTOTEST_START:
                printf("%s voipmsg id is VOIP_UA_EVENT_AUTOTEST_START \n", __FUNCTION__);
#if LV_WATCH_AUTO_TEST != 0
                if(NULL == modem_adp_video_test_task_id) {
                    modem_adp_video_test_task_id = uos_create_task(MMI_ModemAdp_Video_Call_Test_Main, NULL, 64, 4 * 1000,
                                                                   MODEM_ADP_TASK_PRIORITY, "modem_adpator_video_call_test");
                    uos_sleep(5);  // to start test task
                }
#endif
                if(0 == Hal_Pm_Get_State()) Hal_Pm_WakeUp();
                MMI_Modem_Video_Call_Common_Response(MMI_MODEM_VIDEO_CALL_RESPONSE_AUTO_TEST);
                break;
            default:
                printf("%s voipmsg id is %d\n", __FUNCTION__, voipmsg.msgid);
                break;
        }
    }
} /* MMI_ModemAdp_Video_Call_Main */

/**
 * This is used to get the reg state of video call.
 * param stat:  reg status
 * return  void
 */
UINT8 MMI_Modem_Video_Call_Get_Reg_Stat_Req(void)
{
    return(g_ModemVcRegStat);
}

/**
 * This is used to start  video call
 * param calltype: MODEM_VIDEO_CALL_TYPE
 * param pContactUri: char*
 * return  UINT32
 */
INT32 MMI_Modem_Video_Call_Dial_Req(MODEM_VIDEO_CALL_TYPE calltype, char * pContactUri)
{
    printf("#interface %s\n", __FUNCTION__);
    bsapi_call_dial(calltype, pContactUri);

    return 1;
}

/**
 * This is used to control video call action
 * param op: UINT16
 * param vidType: UINT16
 * return  UINT32
 */
UINT32 MMI_Modem_Video_Call_Action_Req(MODEM_VIDEO_CALL_ACTION op,
                                       MODEM_VIDEO_CALL_BARESIPVIDMODE vidType)
{
    printf("#interface %s\n", __FUNCTION__);
    bsapi_call_action(op, vidType);
    return 1;
}

/**
 * This is used to set local preview
 * param stat:
 * return  void
 */
VOID MMI_Modem_Video_Call_Set_Local_Preview_Req(BOOL localpreview)
{
    baresip_set_localpreview(localpreview);
}

#if LV_WATCH_AUTO_TEST != 0
static void MMI_ModemAdp_Video_Call_Test_Main(void * unused)
{
    voipautotestmsg_t voipautotestmsg;

    while(gui_task_id == NULL) {
        printf("gui_task_id is NULL");
        uos_sleep(10);
    }

    // get msg
    while(1) {
        if(voipAutoTestMsgQueReceive(&voipautotestmsg)) {
            printf("voip test Q is not ready, sleep for 500ms\n");
            uos_sleep(100);
            continue;
        }

        printf("voipautotestmsg_t.msgid is %d\n", voipautotestmsg.msgid);
        if(0 == Hal_Pm_Get_State()) Hal_Pm_WakeUp();
        switch(voipautotestmsg.msgid) {
        case VOIP_UA_EVENT_AUTOTEST_START:
            printf("VOIP_UA_EVENT_AUTOTEST_START \n");
            break;
        case VOIP_UA_EVENT_AUTOTEST_STOP:
            printf("VOIP_UA_EVENT_AUTOTEST_STOP \n");
            MMI_Modem_Video_Call_Common_Response(MMI_MODEM_VIDEO_CALL_RESPONSE_TEST_STOP);
            break;
        case VOIP_UA_EVENT_AUTOTEST_VOICE_MO_REQ:
            printf("VOIP_UA_EVENT_AUTOTEST_VOICE_MO_REQ \n");
            MMI_Modem_Video_Call_Response_Test_Voice_Mo_Req(voipautotestmsg.uri);
            break;
        case VOIP_UA_EVENT_AUTOTEST_ACCEPT_MT_CALL:
            printf("VOIP_UA_EVENT_AUTOTEST_ACCEPT_MT_CALL \n");
            MMI_Modem_Video_Call_Common_Response(MMI_MODEM_VIDEO_CALL_RESPONSE_TEST_MT_CALL);
            break;
        case VOIP_UA_EVENT_AUTOTEST_VIDEO_MO_REQ:
            printf("VOIP_UA_EVENT_AUTOTEST_VOICE_MO_REQ \n");
            MMI_Modem_Video_Call_Response_Test_Video_Mo_Req(voipautotestmsg.uri);
            break;
        case VOIP_UA_EVENT_AUTOTEST_REJECT_CALL:
            printf("VOIP_UA_EVENT_AUTOTEST_REJECT_CALL \n");
            MMI_Modem_Video_Call_Common_Response(MMI_MODEM_VIDEO_CALL_RESPONSE_TEST_REJ_CALL);
            break;
        case VOIP_UA_EVENT_AUTOTEST_CMD_BEGIN:
        case VOIP_UA_EVENT_AUTOTEST_CMD_END:
        default:
            break;
        }
    }
}
#endif //LV_WATCH_AUTO_TEST

#endif //USE_LV_WATCH_VOIP_ADAPTOR
